#include "MyUI.h"


using namespace Ogre;
using namespace OIS;

MyUI::MyUI( Mouse* mouse )
: mpMouse( mouse ),
  mrManager( OverlayManager::getSingleton() ),
  mpFormLayer( NULL ),
  mpCursorLayer( NULL ),
  mpCursor( NULL ),
  mCursorX( 0 ),
  mCursorY( 0 ),
  mMouseLeftButtonPressed( false ),
  mpClickedElement( NULL ),
  mpDragElement( NULL ),
  mForms(),
  mpFocusForm( NULL ),
  mpBackpackForm( NULL )
{
	mpFormLayer = mrManager.create( "FormLayer" );
	mpFormLayer->setZOrder( 100 );
	mpFormLayer->show();

	mpCursorLayer = mrManager.create( "CursorLayer" );
	mpCursorLayer->setZOrder( 200 );
	mpCursorLayer->show();

	mpCursor = (OverlayContainer*) mrManager.createOverlayElementFromTemplate( "SdkTrays/Cursor", "Panel", mpCursorLayer->getName() + "/Cursor" );
	mpCursorLayer->add2D( mpCursor );

	BackpackForm_Initialize();
}

MyUI::~MyUI()
{
	mrManager.destroy( mpFormLayer );
	mrManager.destroy( mpCursorLayer );
}

bool MyUI::OnFrame( const FrameEvent& evt )
{
	//UpdateCursor();

	return true;
}

bool MyUI::OnMouseDown( const MouseEvent& evt, MouseButtonID id )
{
	UpdateCursor();

	if( id == MB_Left ) mMouseLeftButtonPressed = true;

	if( mpBackpackForm->contains( mCursorX, mCursorY ) ) BackpackForm_OnMouseDown( evt, id );

	return true;
}

bool MyUI::OnMouseUp( const MouseEvent& evt, MouseButtonID id )
{
	UpdateCursor();

	if( id == MB_Left ) mMouseLeftButtonPressed = false;

	if( mpBackpackForm->contains( mCursorX, mCursorY ) ) BackpackForm_OnMouseUp( evt, id );

	return true;
}

bool MyUI::OnMouseMove( const MouseEvent& evt )
{
	UpdateCursor();

	if( mpBackpackForm->contains( mCursorX, mCursorY ) ) BackpackForm_OnMouseMove( evt );

	BackpackForm_OnMouseMove( evt );
	return true;
}

void MyUI::UpdateCursor()
{
	mpCursor->setPosition( mpMouse->getMouseState().X.abs, mpMouse->getMouseState().Y.abs );

	mCursorX = mpCursor->_getLeft();
	mCursorY = mpCursor->_getTop();
}

void MyUI::BackpackForm_Initialize()
{
	mpBackpackForm = (OverlayContainer*)mrManager.createOverlayElementFromTemplate( "MyUI/Tray", "BorderPanel", mpFormLayer->getName() + "/Backpack" );
	mpFormLayer->add2D( mpBackpackForm );

	int width  = 48;
	int height = 48;
	int x = 4;
	int y = 4;
	for( int i = 0; i < 16; ++i )
	{
		OverlayContainer* pPanel = (OverlayContainer*)mrManager.createOverlayElementFromTemplate( "SdkTrays/Tray", "BorderPanel", mpBackpackForm->getName() + "/Item" + StringConverter::toString( i ) );

		pPanel->setDimensions( width, width );
		pPanel->setPosition( x + ( ( i / 4 ) * ( width + 1 ) ), y + ( ( i % 4 ) * ( height + 1 ) ) );

		mpBackpackForm->addChild( pPanel );
	}

	mpBackpackForm->setPosition( 100, 100 );
	mpBackpackForm->setColour( Ogre::ColourValue( 0, 0, 0, 1 ) );
	mpBackpackForm->setDimensions( x * 2 + 4 * ( width + 1 ), y * 2 + 4 * ( height + 1 ) );
}

void MyUI::BackpackForm_OnMouseDown( const OIS::MouseEvent& evt, OIS::MouseButtonID id )
{
	OverlayElement* pPanel = mpBackpackForm->findElementAt( mCursorX, mCursorY );

	if( pPanel && pPanel != mpBackpackForm )
	{
		if( id == MB_Left )
		{
			pPanel->setMaterialName( "SdkTrays/Shade" );

			mpClickedElement = pPanel;
		}
		else if( id == MB_Right )
		{
			pPanel->setMaterialName( "SdkTrays/Tray" );
		}
	}
}

void MyUI::BackpackForm_OnMouseUp( const OIS::MouseEvent& evt, OIS::MouseButtonID id )
{
}

void MyUI::BackpackForm_OnMouseMove( const OIS::MouseEvent& evt )
{
	if( mMouseLeftButtonPressed && mpDragElement == NULL )
	{
		OverlayElement* pPanel = mpBackpackForm->findElementAt( mCursorX, mCursorY );

		if( pPanel && pPanel != mpClickedElement )
		{
			mpDragElement = mpClickedElement;
			mpCursor->setMaterialName( mpDragElement->getMaterialName() );
		}
	}
}
